package edu.office.applicationRepository;

import edu.office.entities.Knowledge;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.io.Serializable;
import java.util.List;

/**
 *
 * @author Administrator
 */
@Named("robotAnswerController")
@ApplicationScoped
public class RobotAnswerRepository implements Serializable {

    @Inject
    KnowledgeRepository applicationKnowledgeController;
    public String getAnswer(String question) {
        String answer;
        if (question.trim().length() > 0) {
            //从数据库Knowledge中查询获得相关的知识点
            StringBuilder sqlString = new StringBuilder();
            sqlString.append("SELECT * FROM LEARNING.KNOWLEDGE where name='");
            sqlString.append(question);
            sqlString.append("' or locate(details,'");
            sqlString.append(question);
            sqlString.append(">0");
            List<Knowledge> knowledges = applicationKnowledgeController.list(sqlString.toString());
            if (!knowledges.isEmpty()) {
                answer = knowledges.get(0).getDETAILS();
            } else {
                answer = "No answer";
            }
            return answer;
        } else {
            return "";
        }
    }
}
